﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Private_ChangePassword : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
    }
    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle("Change Password");
    }
    protected void ChangePassword()
    {
        App.Data.User user = SessionCache.CurrentUser;
        if (String.Compare(user.Password, txtOldPassword.Text, false) != 0)
            WebUtil.ShowMessageBox(divMessage, "Invalid Old Password.", true);
        else
        {
            BtcHousingEntities context = new BtcHousingEntities();
            var currentUser = context.Users.FirstOrDefault(P => P.ID == user.ID);
            currentUser.Password = txtNewPassword.Text;
            context.SaveChanges();

            SessionCache.CurrentUser.Password = currentUser.Password;
            WebUtil.ShowMessageBox(divMessage, "Your Password has been Changed Successfully.", false);
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            ChangePassword();
        }
    }
}